package 语法.解析数据


import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import 语法.面向对象.元编程.Person

//对象转化为json字符串
def list = [new Person(name: "jml",age: 18),new Person(name: "jml001",age: 19)]
def json = JsonOutput.toJson(list)
println json

//json转对象
//json转对象转对象后不用得到具体的实体，可以直接调用属性
def j1= "[{\"age\":18,\"name\":\"jml\"},{\"age\":19,\"name\":\"jml001\"}]"
def jsonSlurper = new JsonSlurper();
list = jsonSlurper.parseText(j1);
println list[0]
println list.class

//groovy使用java的第三方类库
//将jar包拷贝到libs目录下，右键加入jar包，

def getNetworkDate(String url){
    //发送http请求
    def connection = new URL(url).openConnection();
    connection.setRequestProperty("GET")
    connection.connect()
    def response = connection.content.text;
    //将json解析转化为实体对象
    def jsonSlurper = new JsonSlurper();
    return jsonSlurper.parseText(response);
}



